package com.auctionhaus



class Listing {

    String listName
    Date endDateTime
    Double startBidPrice
    Double maxBidPrice
    Double nextBidPrice
    String description
    Customer winner
    Customer seller

    static hasMany = [bids:Bid]

    static transients = [ 'maxBidPrice', 'nextBidPrice']

    static constraints = {
      description(nullable:true, size:0..256)
      listName(size:1..64)
      seller(nullable:false)
      winner(nullable:true)
      startBidPrice(nullable:false )
      endDateTime(nullable:false, validator: { val, obj -> obj.properties['endDateTime'] >= new Date()
                })
    }

     Double getNextBidPrice() {
           getMaxBidPrice() + Bid.incrementRate
     }
    Double getMaxBidPrice() {

           if (this.bids?.size() > 0)
                return this.bids.collect{bid -> bid.amount}.max()

           return startBidPrice
    }
    static mapping = {
        seller cascade:"none"
    }

}
